package com.xr.blog.controller;

import com.xr.blog.pojo.BlogMenu;
import com.xr.blog.service.BlogMenuService;
import com.xr.blog.tools.Result;
import com.xr.blog.tools.constant.Code;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 菜单表
 *
 * @author XRJ
 * @email 1033835209@qq.com
 * @date 2020-07-07 20:31:38
 */
@RestController
@RequestMapping("/squirrelblog")
public class BlogMenuController {
    @Autowired
    private BlogMenuService blogMenuService;


    @GetMapping(value = "/menu/list",produces = "application/json;charset=UTF-8")
    public Result<List<BlogMenu>> list() {
        return Result.of(Code.SC_OK.getState(),"查询成功",blogMenuService.list());
    }


    @PostMapping(value = "/menu/save",produces = "application/json;charset=UTF-8")
    public Result<BlogMenu> save(@RequestBody @Validated({Update.class, Insert.class}) BlogMenu menu){
        //ValidatorUtils.validate(menu);
        blogMenuService.saveOrUpdate(menu);
        return Result.of(Code.SC_OK.getState(),"保存成功",menu);
    }



    @GetMapping(value = "/menu/getById",produces = "application/json;charset=UTF-8")
    public Result<BlogMenu> info(Long id){
        return Result.of(Code.SC_OK.getState(),"查询成功",blogMenuService.getById(id));
    }

    @DeleteMapping(value = "/menu/delete",produces = "application/json;charset=UTF-8")
    public Result remove(Long id){
        return blogMenuService.removeById(id)?Result.of(Code.SC_OK.getState(),"删除成功",null):Result.of(Code.DEL_FAIL.getState(),"删除失败",null);
    }
}
